package dao

import "WORKSPACE/models"

func GetAllconsumer(consumerParam map[string]interface{}) (error, []models.Consumer, int64) {
	var consumerData []models.Consumer
	page := consumerParam["page"].(int)
	pageSize := consumerParam["limit"].(int)
	searchName := consumerParam["searchName"].(string)
	var total int64
	err := db.Table("consumer").Where("g.goods_name like ? and g.is_del = false", searchName+"%").Order("goods_id ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&consumerData).Error
	return err, consumerData, total
}

func Addconsumer(consumer models.Consumer) error {
	err := db.Table("consumer").Select("consumer_id","consumer_name","consumer_address","consumer_poscode","consumer_phone").Create(&consumer).Error
	return err
}

func Updateconsumer(consumer models.Consumer) error {
	err := db.Table("consumer").Where("consumer_id = ?", consumer.Consumer_id).Updates(&consumer).Error
	return err
}

func Delconsumer(id int) error {
	err := db.Table("consumer").Where("consumer_id = ?", id).Delete(models.Consumer{}).Error
	return err
}